package se.webbzon.boltzmann.minigame.sandbox;

public class DefaultSandBoxItem implements SandBoxItem {
	
	// The instance attached to this abstract sand box item
	private SandBoxObject instance;
	
	// Whatever the instance is draggable and/or droppable
	private boolean draggable, droppable;
	
	/*============================================================
	Constructors
	============================================================*/
	
	public DefaultSandBoxItem() {
		this(null);
	}
	
	public DefaultSandBoxItem(SandBoxObject instance) {
		this.instance = instance;
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	@Override public boolean draggable() {
		return draggable;
	}

	@Override public boolean droppable() {
		return droppable;
	}

	@Override public void setPosition(double x, double y) {
		instance.setPosition(x, y);
	}
	
	@Override public double getX() {
		return instance.getX();
	}

	@Override public double getY() {
		return instance.getY();
	}

	@Override public double getWidth() {
		return instance.getWidth();
	}

	@Override public double getHeight() {
		return instance.getHeight();
	}
	
	@Override public void onAttach(SandBox box) {
		if (!instance.exists()) {
			box.getWorld().createInstance(instance);
			instance.onAttach(box);
		}
	}

	@Override public void onDetach(SandBox box) {
		if (instance.exists()) {
			box.getWorld().destroyInstance(instance);
			instance.onDetach(box);
		}
	}
	
	/*============================================================
	Protected Methods
	============================================================*/
	
	/** Sets whatever this sand box item is draggable or not. **/
	protected void setDraggable(boolean draggable) {
		this.draggable = draggable;
	}
	
	/** Sets whatever this sand box item is droppable or not. **/
	protected void setDroppable(boolean droppable) {
		this.droppable = droppable;
	}
	
	/** Sets the size of this abstract sand box item. **/
	protected void setSize(double width, double height) {
		instance.setSize(width, height);
	}
	
	/** Sets the instance attached to this abstract sand box item. This method
	 * should only be used in the constructor of the sand box item. **/
	protected void setInstance(SandBoxObject instance) {
		this.instance = instance;
	}
	
	/** Returns the instance attached to this abstract sand box item. **/
	protected SandBoxObject getInstance() {
		return instance;
	}

}
